﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FotoBrowserWPF.View;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.Windows.Data;

namespace FotoBrowserWPF.Controler
{
    public class FotoBrowserControler
    {
        public FotoBrowserModel Model { get; set; }
        public IFotoBrowserView View { get; set; }

        public FotoBrowserControler(
            IFotoBrowserView view,
            FotoBrowserModel model)
        {
            this.Model = model;
            this.View = view; 
        }

        public void Init()
        {
            this.View.ViewData = new FotoBrowserViewData();
            this.View.DataContext = this.View.ViewData;
            this.View.Click_Next += new System.Windows.RoutedEventHandler(View_Click_Next);
            this.View.Click_Previous += new System.Windows.RoutedEventHandler(View_Click_Previous);
            this.View.ViewData.FotoMetadatas = Model.LoadFilesFromFileSystem();
            this.View.ViewData.ImageSource =
                FotoBrowserHelper.GetImage(this.View.ViewData.FotoMetadatas[0].Path);
        }

        public void View_Click_Previous(object sender, System.Windows.RoutedEventArgs e)
        {
            ICollectionView view = GetMetdataView();
            view.MoveCurrentToPrevious();
            if (view.IsCurrentBeforeFirst)
            {
                view.MoveCurrentToFirst();
            }
            this.View.ViewData.ImageSource =
              FotoBrowserHelper.GetImage(((FotoMetadata)view.CurrentItem).Path);
        }

        public void View_Click_Next(object sender, System.Windows.RoutedEventArgs e)
        {
            ICollectionView view = GetMetdataView();
            view.MoveCurrentToNext();
            if (view.IsCurrentAfterLast)
            {
                view.MoveCurrentToLast();
            }
            this.View.ViewData.ImageSource =
               FotoBrowserHelper.GetImage(((FotoMetadata)view.CurrentItem).Path.ToLower());
        }


        ICollectionView GetMetdataView()
        {
            return CollectionViewSource.GetDefaultView(this.View.ViewData.FotoMetadatas);
        }

    }
}
